package com.mesSystem.controller;


import com.mesSystem.dto.ProcessWorkstationRequest;
import com.mesSystem.dto.Result;
import com.mesSystem.entity.ProcessWorkstation;
import com.mesSystem.entity.WorkstationType;
import com.mesSystem.service.impl.ProcessWorkstationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Slf4j
@RequestMapping("processWorkstation")
public class ProcessWorkstationManagementController {
    @Autowired
    ProcessWorkstationService processWorkstationService;

    /**
     * 管理（添加、删除）工序工位类型信息
     *
     * @param request
     * @return
     */
    @PostMapping("management")
    public Result Management(@RequestBody ProcessWorkstationRequest request) {
        // 从map中取出工位类型和对应的数量
        Map<Long, Integer> workstationTypes = request.getWorkstationTypeMap();

        // 创建一个ProcessWorkstation对象保存信息
        ProcessWorkstation processWorkstation = new ProcessWorkstation();
        processWorkstation.setProcessId(request.getProcessId());

        // 使用entrySet()方法遍历Map
        for (Map.Entry<Long, Integer> entry : workstationTypes.entrySet()) {
            processWorkstation.setWorkstationTypeId(entry.getKey());
            processWorkstation.setQuantity(entry.getValue());
            // 每取出一个就存入一次到工序工位管理中
            processWorkstationService.save(processWorkstation);
        }
        return Result.ok("编辑成功");
    }

    /**
     * 根据工序名称查询该工序包含的工位类型
     * @param page
     * @param size
     * @param processId
     * @return
     */
    @GetMapping("queryByProcessId")
    public Result queryWorkstation(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                   @RequestParam(value = "size", defaultValue = "10") Integer size,
                                   @RequestParam(value = "processId") Long processId) {
        // 根据工序id，取出其所使用的工位类型。
        return Result.ok(processWorkstationService.queryByProcessId(page, size, processId));
    }


}
